<!-- BusFullReduction -->
<template>
  <div>
    <div class="topSearch">
      <div class="topSearchLeft">
        <el-row :gutter="20">
          <el-form :inline="true">
            <el-row class="btnRow">
              <el-col :span="6" class="elrow">
                <el-form-item>
                  <p>开启状态:</p>
                  <el-select
                    v-model="isOpen"
                    :style="{ width: '160px' }"
                    clearable
                    filterable
                    placeholder="开启状态"
                    @clear="ClearCategory"
                  >
                    <el-option
                      v-for="(item, i) in activeisOpen"
                      :key="i"
                      :label="item.name"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="elrow">
                <el-form-item>
                  <p>活动状态:</p>
                  <el-select
                    v-model="status"
                    :style="{ width: '160px' }"
                    clearable
                    filterable
                    placeholder="活动状态"
                    @clear="ClearCategory"
                  >
                    <el-option
                      v-for="(item, i) in goodsStatusList"
                      :key="i"
                      :label="item.name"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6" class="elrow">
                <el-form-item>
                  <p>活动名称:</p>
                  <el-input
                    v-model="activeName"
                    :style="{ width: '160px' }"
                    placeholder="活动名称"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" class="elrow">
                <!-- <el-form-item>
                  <p>订单状态:</p>
                  <el-select
                    v-model="orderStatus"
                    placeholder="订单状态"
                    filterable
                    clearable
                    @clear="ClearAttributes"
                    :style="{ width: '200px', marginLeft: '10px' }"
                  >
                    <el-option
                      v-for="(item, index) in AttributesSelect"
                      :key="index"
                      :label="item.name"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item> -->
              </el-col>
            </el-row>
            <el-row :class="!showAll ? 'btnRow' : 'unBtnRow'">
              <el-col :span="7" style="width: 500px; margin-top: -30px">
                <el-form-item>
                  <p style="margin-right: 10px">日期选择:</p>
                  <el-date-picker
                    :style="{ width: '500px', marginLeft: '-10px' }"
                    v-model="searchTime"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    value-format="yyyy-MM-dd hh:mm:ss"
                    end-placeholder="结束日期"
                    @change="chooseTimeHandle"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>

        <!-- <el-form-item> </el-form-item> -->
      </div>
      <div class="topSearchRight">
        <el-button
          type="primary"
          :style="{ marginLeft: '20px' }"
          @click="handleSearch"
        >
          查询
        </el-button>
        <el-button :style="{ marginLeft: '20px' }" @click="resettingHandle">
          重置
        </el-button>
        <el-row class="expand">
          <el-col :span="1.5" class="elrowBtn">
            <el-button type="text" size="max" @click="closeSearch">
              {{ word }}
              <i
                :class="showAll ? 'el-icon-arrow-down ' : 'el-icon-arrow-up'"
              ></i>
            </el-button>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="fullreductionbox">
      <!-- <div>
        <el-select
          v-model="status"
          :style="{ width: '160px', marginLeft: '20px' }"
          clearable
          filterable
          placeholder="活动状态"
          @clear="ClearCategory"
        >
          <el-option
            v-for="(item, i) in goodsStatusList"
            :key="i"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="isOpen"
          :style="{ width: '160px', marginLeft: '20px' }"
          clearable
          filterable
          placeholder="开启状态"
          @clear="ClearCategory"
        >
          <el-option
            v-for="(item, i) in activeisOpen"
            :key="i"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input
          v-model="activeName"
          :style="{ width: '160px', marginLeft: '20px' }"
          placeholder="活动名称"
        ></el-input>
        <el-date-picker
          :style="{ width: '500px', marginLeft: '20px' }"
          v-model="searchTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          value-format="yyyy-MM-dd hh:mm:ss"
          end-placeholder="结束日期"
          @change="chooseTimeHandle"
        >
        </el-date-picker>
        <el-button
          :style="{ marginLeft: '20px' }"
          circle
          icon="el-icon-search"
          @click="handleSearch"
        ></el-button>
      </div> -->
      <!-- <div class="SecondRow">
        <div>
          <el-button round size="mini" @click="addgoods">新增活动</el-button>
          <el-button :style="{ marginLeft: '20px' }" round size="mini"
            >导出
          </el-button> 
          <el-button
            :style="{ marginLeft: '20px' }"
            round
            size="mini"
            @click="Refresh"
            >刷新
          </el-button>
        </div>
      </div> -->
      <div class="SecondRow">
        <div class="SecondRowLeft">
          <el-button type="primary" @click="addgoods"> 新增活动 </el-button>
        </div>
        <div class="SecondRowRight">
          <el-button type="primary" plain> 导出 </el-button>
          <el-button circle icon="el-icon-refresh-right" @click="Refresh">
          </el-button>
          <!-- <el-button round size="mini" @click="addgoods">添加商品</el-button> -->
        </div>
      </div>
      <div class="DataTableDiv">
        <div></div>
        <el-table
          v-loading="loading"
          :cell-style="{ 'text-align': 'center' }"
          :data="goodsList"
          :header-cell-style="{ 'text-align': 'center' }"
          border
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column type="selection" width="55"></el-table-column> -->
          <el-table-column fixed label="排序" prop="sort" min-width="70">
            <template slot-scope="scope">
              <div class="flex-container">
                <span class="flex-item">
                  <!-- 序号 -->
                  {{ scope.$index + 1 }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="活动标题" prop="activeName" min-width="150">
          </el-table-column>
          <el-table-column
            label="活动所属"
            prop="activeAttribution"
            min-width="150"
          >
            <template slot-scope="scope">
              {{ getActiveAttributionText(scope.row.activeAttribution) }}
            </template>
          </el-table-column>
          <el-table-column label="开始时间" prop="startTime" min-width="250">
          </el-table-column>
          <el-table-column label="结束时间" prop="endTime" min-width="250">
          </el-table-column>
          <el-table-column label="是否开启" prop="isOpen" min-width="130">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isOpen"
                :active-value="1"
                :inactive-value="0"
                @change="handleSwitchChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="活动状态" prop="status" min-width="130">
            <template slot-scope="scope">
              {{ getStatusText(scope.row.status) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="400">
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                @click="participateHandle(scope.row)"
                >参加活动</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="editHandle(scope.row)"
                v-if="
                  scope.row.activeAttribution === '2' &&
                  scope.row.status === '1'
                "
                >编辑</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="chakanHandle(scope.row)"
                >查看</el-button
              >
              <!-- <el-button
                size="small"
                type="text"
                @click="applyForHandle(scope.row)"
                >申请列表</el-button
              > -->
              <!-- <el-button
                size="small"
                type="text"
                @click="cancelActiveHandle(scope.row)"
                v-show="scope.row.status !== '3'"
                >取消</el-button
              > -->
              <el-button
                size="small"
                type="text"
                @click="ActiveGoodsHandle(scope.row)"
                >活动商品</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="ActivelinkHandle(scope.row)"
                >活动链接</el-button
              >
              <el-button
                size="small"
                type="text"
                @click="StatisticHandle(scope.row)"
                >统计信息</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- <div class="paging">
        <el-pagination
          :current-page.sync="currentPage"
          :page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 40]"
          layout="sizes,total, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div> -->
      <div class="paging">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 添加/修改商品组件 -->
    <NewActivity
      v-if="centerDialogVisible"
      :centerDialogVisible="centerDialogVisible"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel"
      @Refresh="Refresh"
    />
    <ApplyFor
      v-if="centerDialogVisible2"
      :centerDialogVisible="centerDialogVisible2"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel2"
      @Refresh="Refresh"
    />
    <Statistic
      v-if="centerDialogVisible3"
      :centerDialogVisible="centerDialogVisible3"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel3"
      @Refresh="Refresh"
    />
    <Activelink
      v-if="centerDialogVisible4"
      :centerDialogVisible="centerDialogVisible4"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel4"
      @Refresh="Refresh"
    />
    <ParticipateIn
      v-if="centerDialogVisible5"
      :centerDialogVisible="centerDialogVisible5"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel5"
      @Refresh="Refresh"
    />
    <ActiveGoods
      v-if="centerDialogVisible6"
      :centerDialogVisible="centerDialogVisible6"
      :currentRow="currentRow"
      :enterType="enterType"
      @cancelgoodesmodel="cancelgoodesmodel6"
      @Refresh="Refresh"
    />
  </div>
</template>
<script>
import NewActivity from "@/views/mall/managementend/marketing/components/Newactivity.vue";
import ApplyFor from "@/views/mall/managementend/marketing/components/Applyfor.vue";
import Statistic from "@/views/mall/managementend/marketing/components/Statistic.vue";
import Activelink from "@/views/mall/managementend/marketing/components/Activelink.vue";
import ParticipateIn from "@/views/mall/managementend/marketing/components/Participate.vue";
import ActiveGoods from "@/views/mall/managementend/marketing/components/Activegoods.vue";

import {
  QueryActiveList,
  EditActiveFun,
  EditSwitchFun,
  CancelActivity,
} from "@/api/mallApi/managementend/marketing";

export default {
  name: "BusFullReduction",
  components: {
    NewActivity,
    ApplyFor,
    Statistic,
    Activelink,
    ParticipateIn,
    ActiveGoods,
  },
  data() {
    return {
      showAll: true,
      // 添加/修改 商品弹窗
      centerDialogVisible: false,
      centerDialogVisible2: false,
      centerDialogVisible3: false,
      centerDialogVisible4: false,
      centerDialogVisible5: false,
      centerDialogVisible6: false,
      currentRow: {}, // 保存当前行数据
      enterType: 0, // 默认值为1，表示从编辑按钮进入
      Attributes: null, // 商品属性
      searchShop: "",
      goodsList: [],
      classShop: [],
      // 商品分类列表
      goodsTypeList: [],
      selectedRows: [], // 表格 多选内容
      isButtonDisabled: false, // checkbox 联动按钮
      currentPage: 1, // 分页
      total: 0,
      pageSize: 10,
      loading: false,
      name: "",
      parentCategoryId: "",
      categoryId: "",
      ptStatus: "",
      // 商品属性列表 1：待审核2：审核通过3：驳回 4：违规下架
      goodsStatusList: [
        //1-报名中；2-活动中；3-活动结束；4-活动关闭；5-平台取消
        { name: "报名中", value: 1 },
        { name: "活动中", value: 2 },
        { name: "活动结束", value: 3 },
        { name: "活动关闭", value: 4 },
        { name: "平台取消", value: 5 },
      ],
      activeisOpen: [
        //1-报名中；2-活动中；3-活动结束；4-活动关闭；5-平台取消
        { name: "开启", value: 1 },
        { name: "关闭", value: 0 },
      ],
      searchTime: "",
      activeName: null,
      isOpen: null,
      status: null,
      // startTime: null,
      // endTime: null,
    };
  },
  created() {},
  mounted() {
    this.Refresh();
  },
  methods: {
    resettingHandle() {
      this.searchTime = "";
      this.activeName = null;
      this.isOpen = null;
      this.status = null;
      this.Refresh();
    },
    cancelActiveHandle(row) {
      this.$confirm("是否取消该商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            id: row.id,
          };
          CancelActivity(data)
            .then((res) => {
              if (res.code !== "00000") return;
              this.getGoodsList();
              this.$message({
                type: "success",
                message: "取消成功!",
              });
              console.log(res);
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },
    applyForHandle(row) {
      this.currentRow = row;
      this.centerDialogVisible2 = true; // 打开弹窗
    },
    StatisticHandle(row) {
      this.currentRow = row;
      this.centerDialogVisible3 = true; // 打开弹窗
    },
    ActivelinkHandle(row) {
      this.currentRow = row;
      console.log("点击按钮打开", row, this.currentRow);
      this.centerDialogVisible4 = true; // 打开弹窗
    },
    participateHandle(row) {
      this.currentRow = row;
      this.centerDialogVisible5 = true; // 打开弹窗
    },
    ActiveGoodsHandle(row) {
      this.currentRow = row;
      this.centerDialogVisible6 = true; // 打开弹窗
    },
    chakanHandle(row) {
      this.enterType = 3;
      this.currentRow = row;
      this.centerDialogVisible = true; // 打开弹窗
    },
    editHandle(row) {
      this.enterType = 2;
      this.currentRow = row;
      this.centerDialogVisible = true; // 打开弹窗
    },
    chooseTimeHandle(e) {},
    Refresh() {
      this.getGoodsList();
    },
    getStatusText(Status) {
      //1-报名中；2-活动中；3-活动结束；4-活动关闭；5-平台取消
      switch (Status) {
        case "1":
          return "报名中";
        case "2":
          return "活动中";
        case "3":
          return "活动结束";
        case "4":
          return "活动关闭";
        case "5":
          return "平台取消";
        default:
          return "未知";
      }
    },
    getActiveAttributionText(activeAttribution) {
      //1-报名中；2-活动中；3-活动结束；4-活动关闭；5-平台取消
      switch (activeAttribution) {
        case "1":
          return "平台";
        case "2":
          return "店铺";
        default:
          return "未知";
      }
    },
    ClearCategory() {
      this.Refresh();
    }, // 清空 商品分类
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      // 分页
      this.currentPage = val;
      this.getGoodsList();
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
      // TODO 在这里进行相应的操作，如发起请求等
    }, // 表格多选
    cancelgoodesmodel(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible = data;
      this.enterType = 0;
    },
    cancelgoodesmodel2(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible2 = data;
    },
    cancelgoodesmodel3(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible3 = data;
    },
    cancelgoodesmodel4(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible4 = data;
    },
    cancelgoodesmodel5(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible5 = data;
    },
    cancelgoodesmodel6(data) {
      // 弹窗关闭时触发，可以在这里重置数据或进行其他操作
      this.currentRow = {};
      this.centerDialogVisible6 = data;
    },
    addgoods() {
      this.enterType = 1;
      this.centerDialogVisible = true; // 打开弹窗
    }, // 添加商品
    // 获取商品列表
    getGoodsList() {
      this.loading = true;
      const params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        activeName: this.activeName,
        isOpen: this.isOpen,
        status: this.status,
        startTime: this.searchTime ? this.searchTime[0] : "",
        endTime: this.searchTime ? this.searchTime[1] : "",
        activityCategory: 1,
      };
      QueryActiveList(params)
        .then((res) => {
          if (res.code !== "00000") return;
          this.goodsList = res.data.rows;
          this.total = parseInt(res.data.totalRows);
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    // 更改商品状态
    handleSwitchChange(row) {
      const data = {
        id: row.id,
        isOpen: row.isOpen,
      };
      EditSwitchFun(data)
        .then((res) => {
          if (res.code !== "00000") return;
          this.$message({
            message: "该活动启用状态更改成功！",
            type: "success",
          });
          this.getGoodsList();
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1;
      this.getGoodsList();
    },
    //展开收起
    closeSearch() {
      this.showAll = !this.showAll;
    },
  },
  computed: {
    word: function () {
      if (this.showAll == false) {
        //对文字进行处理
        return "收起";
      } else {
        return "展开";
      }
    },
    tableHeight() {
      if (this.showAll) {
        return window.innerHeight - 70;
      } else {
        return window.innerHeight - 70;
      }
    },
  },
  watch: {
    selectedRows: {
      handler(newValue) {
        // 数组发生变化时，isButtonDisabled计算属性会重新计算，从而更新按钮的禁用状态
        this.isButtonDisabled = newValue.length === 0;
      },
      deep: true,
      immediate: true, // 立即执行处理函数
    },
  },
};
</script>

<style lang="scss" scoped>
.topSearch {
  width: 98%;
  margin: 1%;
  min-height: 65px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  .topSearchLeft {
    margin-top: 20px;
    margin-left: 30px;
    width: 1200px;
    display: flex;
    ::v-deep .el-row {
      width: 100%;
    }
    ::v-deep .el-col-6 {
      width: 240px;
    }
    ::v-deep .el-form-item__content {
      display: flex;
      align-items: center;
      p {
        display: inline-block;
        // margin-left: 50px;
        min-width: 65px;
        font-size: 14px;
      }
    }
  }
  .topSearchRight {
    width: 15%;
    height: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 10px;
    ::v-deep .expand {
      margin-left: 10px;
      margin-bottom: 5px;
    }
  }
}
.unBtnRow {
  display: none;
}

.fullreductionbox {
  // display: flex;
  // flex-direction: column;
  // align-content: center;
  // justify-content: center;
  width: 98%;
  min-height: 750px;
  margin-top: 1%;
  margin-bottom: 1%;
  margin-left: 1%;
  background-color: #fff;
  overflow: hidden;

  .SecondRow {
    width: 100%;
    min-height: 65px;
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    .SecondRowLeft {
      margin-top: 10px;
      margin-left: 25px;
    }
    .SecondRowRight {
      margin-top: 10px;
      margin-right: 30px;
    }
  }

  .DataTableDiv {
    width: 98%;
    margin-left: 1%;
    margin-bottom: 15px;
    border: 1px solid #dcdee2;

    // > div:nth-child(1) {
    //   padding: 10px;
    // }

    //表格
    .flex-container {
      display: flex;
      align-items: center; /* 垂直居中 */
      justify-content: space-around;

      > img {
        width: 60px;
        height: 60px;
      }

      .flex-item {
        width: 150px;
        height: 40px;
        -webkit-line-clamp: 2; /* 显示两行文本 */
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
    }

    .TableAttrTem {
      display: flex;
      align-items: center;
      // justify-content: space-around;
      margin: 0 25px;
    }
  }

  .paging {
    width: 99%;
    height: 50px;
    margin-bottom: 15px;
    // background-color: antiquewhite;
    display: flex;
    align-items: end;
    justify-content: end;
  }
}
</style>
